﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.IO;
using System.Numerics;

namespace ZhlTextMod.ViewModels
{
    public class TextWindowViewModel : BindableBase
    {
        public string Title { get; set; } = "TextWindow";

        #region DelegateCommand

        public DelegateCommand SpliteTextCommand { get; set; }

        #endregion DelegateCommand

        private string text1;

        public string InputString
        {
            get { return text1; }
            set { SetProperty(ref text1, value); }
        }

        private string text2 = string.Empty;

        public string InputString2
        {
            get { return text2; }
            set { SetProperty(ref text2, value); }
        }

        public TextWindowViewModel()
        {
            SpliteTextCommand = new DelegateCommand(OnSpliteText);
            InputString2 = Guid.NewGuid().ToString();
        }

        private void OnSpliteText()
        {
            if (!string.IsNullOrWhiteSpace(InputString))
            {
                var text1s = InputString;

                MemoryStream stream = new MemoryStream();
                StreamWriter writer = new StreamWriter(stream);
                writer.Write(text1s);
                writer.Flush();
                string _inputStr = "";
                string sp1 = "<a";
                // convert stream to string
                stream.Position = 0;
                StreamReader reader = new StreamReader(stream);
                while (reader.Peek() >= 0)
                {
                    var tx = reader.ReadLine();
                    var hasA = tx.Trim().StartsWith(sp1);
                    if (hasA)
                    {
                        var aspdf = tx.Trim().IndexOf(".pdf", StringComparison.CurrentCultureIgnoreCase);
                        if (aspdf > 0)
                        {
                            _inputStr += tx + System.Environment.NewLine;
                        }
                    }
                }
                InputString2 = _inputStr;
            }           
        }
    }
}